***** Cleaning the raw experimental datafile

cd ""
clear 
set more off

use experimental_data

**** drops  non Canadian citizens 
drop if citizen==0

* 1=choosing the distant or the risky alternative
forval q=1/103 {
	la var D`q' "Decision `q'" 
}

* Intertemporal choices
keep D1-D48
save temporal_choice, replace
outsheet using temporal_choice, comma replace




clear
use experimental_data

**** drops  non Canadian citizens 
drop if citizen==0

* 1=choosing the distant or the risky alternative
forval q=1/103 {
	la var D`q' "Decision `q'" 
}

* Lottery choices
keep D49-D103
save lottery_choice, replace
outsheet using lottery_choice, comma replace





clear
use experimental_data

* drops choice data
drop D1-D103

*After vars are renamed, this default label keeps track of their orignial names
foreach var of varlist _all {
	la var `var'  "`var'"		
}




/****************************************************************************************************************************************************************
																	
																		  IDENTIFIERS
																	
****************************************************************************************************************************************************************/

la var subject "Id (num)"
la var session "Session (num)"

la var nameschool "School name (clean)"

la var schoolid "School id"


/****************************************************************************************************************************************************************
																	
																			 CHOICES
																	
****************************************************************************************************************************************************************/




/****************************************************************************************************************************************************************
																	
Demographic, socio-economic and psychometric variables
																	
****************************************************************************************************************************************************************/


rename schcity A3_schcity
la var A3_schcity "City of school enrolled (brut)"

rename fulltime A4_fulltime

 
la var A4_fulltime "1 full-time student, 0 part-time" 
drop A4

rename A5 A5_grade
la var A5_grade "Grade"

rename REGION_ST A7_region
la var A7_region "Province"



rename female sex 
la var sex "1 if female"




la var citizen "1 if Canadian citizen"


rename B4A B4_language_home
la var B4_language_home "Language sponken home"

**** the language demographic var has 3 categoriess (english, french, other)
replace B4_language_home="1" if B4_language_home=="English"
replace B4_language_home="0" if inlist(B4_language_home, "French", "Other.")
rename B4_language_home english
destring english, replace

**** drops  non Canadian citizens 
drop if citizen==0

**** age really concentrated to a age groups, binarize (15 and 16 is omitted category)
tab age
gen age17= age==17
gen age18= age==18
gen age19plus= age>18
drop age
drop citizen



*C11 labeled as C5 here.... Higher numbers mean more confidence.
local i=1
foreach letter in A B C D E F G {
	local var C5`letter'
	replace `var'="4" if `var'=="Strongly Agree"
	replace `var'="3" if `var'=="Agree"
	replace `var'="2" if `var'=="Disagree"
	replace `var'="1" if `var'=="Strongly Disagree"
	destring `var', replace
	* have wrong orientation (replace as min=max and max=min) - change to negative then add # of categories+1
	if inlist("`letter'", "C", "D", "E") {
		replace `var' =-(`var')+5
		}
	rename `var' confident`i'

	local ++i
	}	




*************** Higher numbers mean more cognitive skills.
rename DQ1 D1_HSgrade
la var D1_HSgrade "HS grade (1=less 50\%)"
replace D1_HSgrade = "1" if D1_HSgrade=="Less than 50%"
replace D1_HSgrade = "2" if D1_HSgrade=="50% to 54%"
replace D1_HSgrade = "3" if D1_HSgrade=="55% to 59%"
replace D1_HSgrade = "4" if D1_HSgrade=="60% to 69%"
replace D1_HSgrade = "5" if D1_HSgrade=="70% to 79%"
replace D1_HSgrade = "6" if D1_HSgrade=="80% to 89%"
replace D1_HSgrade = "7" if D1_HSgrade=="90% to 100%"
destring D1_HSgrade, replace

foreach var of varlist DQ2 DQ3 DQ4 DQ5 DQ6 DQ7 {
	replace `var' = "1" if `var'=="Poor"
	replace `var' = "2" if `var'=="Good"
	replace `var' = "3" if `var'=="Very Good"
	replace `var' = "4" if `var'=="Excellent"
	destring `var', replace
}

local i=1
foreach var of varlist numeracy D1_HSgrade DQ2 DQ3 DQ4 DQ5 DQ6 DQ7 {
	rename `var' cognit`i'
	local ++i
	}


* wants to go to university	
rename D10A D20_levellike
la var D20_levellike "Highest educ would like to get"
gen D20_univ =(D20_levellike=="University first professional degree (eg. medicine  dentistry  veterinary medicine  law  optometry  divinity)") 
replace D20_univ=1 if D20_levellike=="University bachelor's degree (eg. BA  BSc  B.Ed.)" 
replace D20_univ=1 if D20_levellike=="University Master's degree or Ph.D"
la var D20_univ "Highest level of education liked = univ"


** School engagement

* hrs homework per week
* Higher numbers mean more dilligency and restraint (and willingness to learn)
replace E1 = "1" if E1=="Zero hours" | E1=="Less than 1 hour per week"
replace E1 = "2" if E1=="1 to 3 hours"
replace E1 = "3" if E1=="4 to 7 hours"
replace E1 = "4" if E1=="8 to 14 hours"
replace E1 = "5" if E1=="15 hours or more"
destring E1, replace

foreach j in 3 7 4 {
	local var E`j'
	replace `var'="5" if `var'=="All of the time"
	replace `var'="4" if `var'=="Most of the time"
	replace `var'="3" if `var'=="Some of the time"
	replace `var'="2" if `var'=="Rarely"
	replace `var'="1" if `var'=="Never"
	destring `var', replace
	* have wrong orientation (replace as min=max and max=min) - change to negative then add # of categories+1
	if inlist(`j', 3) {
		replace `var' =-(`var')+6
		}


	}		

replace E13 = "1" if E13=="More than once a week"
replace E13 = "2" if E13=="About once a week"
replace E13 = "3" if E13=="Once or twice a month"
replace E13 = "4" if E13=="Less than once a month"
replace E13 = "5" if E13=="Never"
destring E13, replace
	
local i=1
foreach var of varlist E1 E3 E7 {
	rename `var' dilligent`i'	
	local ++i
	}
	
local i=1	
foreach var of varlist E4 E13 {
	rename `var' restraint`i'	
	local ++i
	}



** Attitudes
*****************************************************************************************************************************************************************

**** Higher numbers mean more emotional stability and control
foreach var of varlist G1A G1B G1C G1D G1E G1F G1G {
	replace `var'="1" if `var'=="Strongly Agree"
	replace `var'="2" if `var'=="Agree"
	replace `var'="3" if `var'=="Disagree"
	replace `var'="4" if `var'=="Strongly Disagree"
	destring `var', replace
	}

la var G1A "Little control (1=st. agree)"
la var G1B "No way solve (1=st. agree)"
la var G1C "Little you can do (1=st. agree)"
la var G1D "Often feel helpless (1=st. agree)"
la var G1E "Pushed around (1=st. agree)"
la var G1F "Depends on you (1=st. agree)"
la var G1G "Set your mind (1=st. agree)"

gen G1F_m =-(G1F-5)
la var G1F_m "Depends on you (5=st. agree)"
gen G1G_m=-(G1G-5)
la var G1G_m "Set your mind (5=st. agree)"

local i=1	
foreach var of varlist G1A G1B G1C G1D G1E G1F_m G1G_m{
	rename `var' stable`i'	
	local ++i
	}






	
** SECTION I: dilligence, ambition, and delay of gratification


* high values imply dilligence and delay of gratification
local i=4
foreach letter in a b c f g h i n q {
	local var i`letter'nb
	rename `var' dilligent`i'

	local ++i
	}	

local i=1
foreach letter in d e j k l m o p r s t u {
	local var i`letter'nb
	rename `var' de_grat`i'

	local ++i
	}		
	



** SECTION J: Excitement seeking, optimism, outward orientation attitudes

* high values mean higher outward orientation and self-reported excitement seeking
local i=8
foreach letter in A B C D {
	local var J1`letter'
	replace `var'="1" if `var'=="Strongly Agree"
	replace `var'="2" if `var'=="Agree"
	replace `var'="3" if `var'=="Uncertain"
	replace `var'="4" if `var'=="Disagree"
	replace `var'="5" if `var'=="Strongly Disagree"
	destring `var', replace
	* have wrong orientation (replace as min=max and max=min) - change to negative then add # of categories+1
	if inlist("`letter'", "A", "C") {
		replace `var' =-(`var')+6
		}
	rename `var' stable`i'

	local ++i
	}	

* high values imply a high level of optimism	
local i=1
foreach letter in A B C {
	local var J2`letter'
	replace `var'="1" if `var'=="Never"
	replace `var'="2" if `var'=="Sometimes"
	replace `var'="3" if `var'=="Frequently"
	replace `var'="4" if `var'=="Always"
	destring `var', replace
	rename `var' optimist`i'

	local ++i
	}	

local var J3	
replace `var'="1" if `var'=="Can't be too careful"
replace `var'="2" if `var'=="Don't know"
replace `var'="3" if `var'=="Depends"
replace `var'="4" if `var'=="Most people can be trusted"
destring `var', replace
rename `var' optimist4	

local var J4	
replace `var'="1" if `var'=="Most people would try to take advantage"
replace `var'="2" if `var'=="Don't know"
replace `var'="3" if `var'=="Depends"
replace `var'="4" if `var'=="Most people would try to be fair"
destring `var', replace
rename `var' optimist5	

local var J5	
replace `var'="1" if `var'=="Just look out for themselves"
replace `var'="2" if `var'=="Don't know"
replace `var'="3" if `var'=="Depends"
replace `var'="4" if `var'=="Try to be helpful"
destring `var', replace
rename `var' optimist6


rename (optimist1 optimist2 optimist4 optimist5 optimist6 confident optimist3 confident5) (posit1 posit2 posit3 posit4 posit5 posit6 posit7 posit8)


* high values mean a high value of intrinsic excitement seeking
local i=1
foreach letter in 1C 1F 1I 1G 2B 2C 2D 2E 2I {
	local var J6`letter'
	replace `var'="1" if `var'=="Extremely Unlikely"
	replace `var'="2" if `var'=="Unlikely"
	replace `var'="3" if `var'=="Not Sure"
	replace `var'="4" if `var'=="Likely"
	replace `var'="5" if `var'=="Extremely Likely"
	destring `var', replace
	rename `var' ex`i'

	local ++i
	}	

* high values mean a high value propensity for non-conformity. 
local i=1
foreach letter in 1A 1E 2H 2J {
	local var J6`letter'
	replace `var'="1" if `var'=="Extremely Unlikely"
	replace `var'="2" if `var'=="Unlikely"
	replace `var'="3" if `var'=="Not Sure"
	replace `var'="4" if `var'=="Likely"
	replace `var'="5" if `var'=="Extremely Likely"
	destring `var', replace
	rename `var' rebel`i'

	local ++i
	}
	

* have wrong orientation 
foreach var of varlist rebel1 rebel2{
		replace `var' =-(`var')+6
		
}
		

* high values mean a high value of self-restraint in doing drugs/alcohol, etc. Midway through the question, the variable labelling changes from 1 to 2 and the letters reset
local i=3
foreach letter in 1B 2G {
	local var J6`letter'
	replace `var'="5" if `var'=="Extremely Unlikely"
	replace `var'="4" if `var'=="Unlikely"
	replace `var'="3" if `var'=="Not Sure"
	replace `var'="2" if `var'=="Likely"
	replace `var'="1" if `var'=="Extremely Likely"
	destring `var', replace
	rename `var' restraint`i'

	local ++i
	}	



* hrs homework per week

* 2,6,10 are agreeableness
foreach j in 2 5 6 {
	local var E`j'
	replace `var'="5" if `var'=="All of the time"
	replace `var'="4" if `var'=="Most of the time"
	replace `var'="3" if `var'=="Some of the time"
	replace `var'="2" if `var'=="Rarely"
	replace `var'="1" if `var'=="Never"
	destring `var', replace
	* have wrong orientation (replace as min=max and max=min) - change to negative then add # of categories+1
	* E6 reclassified here
	if inlist(`j', 6) {
		replace `var' =-(`var')+6
		}
	}		

	
* Higher numbers mean more dilligency and restraint (and willingness to learn)
replace L9 = "1" if L9=="Zero" | L9=="Less than one hour per week"
replace L9 = "2" if L9=="1 to 3 hours"
replace L9 = "3" if L9=="4 to 7 hours"
replace L9 = "4" if L9=="8 to 14 hours"
replace L9 = "5" if L9=="15 hours or more"
destring L9, replace

replace L8 = "1" if L8=="Zero" | L8=="Less than one hour per week"
replace L8 = "2" if L8=="1 to 3 hours"
replace L8 = "3" if L8=="4 to 7 hours"
replace L8 = "4" if L8=="8 to 14 hours"
replace L8 = "5" if L8=="15 hours or more"
destring L8, replace

rename (L8 L9) (cre1 cre3)	

foreach j in 8 10 11 12 {
	local var E`j'
	replace `var'="4" if `var'=="Strongly Agree"
	replace `var'="3" if `var'=="Agree"
	replace `var'="2" if `var'=="Disagree"
	replace `var'="1" if `var'=="Strongly Disagree"
	destring `var', replace
	* have wrong orientation (replace as min=max and max=min) - change to negative then add # of categories+1
	if inlist(`j', 8, 11) {
		replace `var' =-(`var')+5
		}


	}		

local i=8
foreach var of varlist E5 E8 {
	rename `var' cre`i'	
	local ++i
	}
	


local i=9	
foreach var of varlist E6 E10 {
	rename `var' posit`i'	
	local ++i
	}
	
rename (rebel4 confident4 D20_univ dilligent2 restraint1 dilligent7) (cre2 cre4 cre5 cre6 cre7 cre10)
	



/****************************************************************************************************************************************************************
																	
																		PARENTAL Variables SURVEY
																	
****************************************************************************************************************************************************************/


destring ageparent, replace

* variable for whether parents consider it being very important for their child to go to universitygen p_uni_imp=(Q3=="4")
gen p_uni_imp=(Q3=="4")
replace p_uni_imp=. if Q3=="#NULL!"


* sometimes folks did not anwer Q5 (then Null (recorded to.) - lose obs with parents, maybe ignore for now
gen Q5_finbarrier = (Q5B_1=="1" | Q5B_2=="1" | Q5B_3=="1" | Q5B_4=="1" | Q5B_5=="1" | Q5B_6=="1" | Q5B_7=="1")
replace Q5_finbarrier = . if Q5=="#NULL!"
order Q5_finbarrier, after(Q5)
la var Q5_finbarrier "1 if financial barrier"

rename Q5_finbarrier fin_prob19



* parental income
gen Q9_income_0_20 = Y10K + Y10K20
gen Q9_income_20_40 = Y20K30 + Y30K40
foreach var of varlist Q9_income_0_20 Q9_income_20_40 {
	replace `var' = 0 if Q9=="2" 
}

gen Q9_income_40_60 = Y40K50 + Y50K60
gen Q9_income_60_80 = Y60K70 + Y70K80
gen Q9_income_80_100 = Y80K100
gen Q9_income_100plus = Y100K120 + Y120K
foreach var of varlist Q9_income_40_60 Q9_income_60_80  Q9_income_80_100 Q9_income_100plus{
	replace `var' = 0 if Q9=="1" //filtre
}


order Q9_income_0_20- Q9_income_100plus, after(Q9C)

foreach j of numlist 0 20 40 60 80 {
	local k = `j' + 20
	la var Q9_income_`j'_`k'  "Parents income `j'-`k'K"
}
la var Q9_income_100plus "Parents income sup 100K"



forval i=7/8 {
	local j=`i'- 6
	gen Q`i'_dropout = (Q`i'=="1")
	la var Q`i'_dropout "1 if parent `j' dropout"

	gen Q`i'_HS = (Q`i'=="2")
	la var Q`i'_HS "1 if parent `j' HS"

	gen Q`i'_vocational = (Q`i'=="3" | Q`i'=="4" | Q`i'=="5")
	la var Q`i'_vocational "1 if parent `j' vocational"

	gen Q`i'_college = (Q`i'=="6" | Q`i'=="7" | Q`i'=="8" | Q`i'=="77") 
	la var Q`i'_college "1 if parent `j' has been to college"			
	
	foreach var of varlist Q`i'_dropout-Q`i'_college {
		replace `var'=. if Q`i'=="#NULL!" | Q`i'=="98" | Q`i'=="99"
	}	
	order Q`i'_dropout- Q`i'_college, after(Q`i')
}

* values 77 (other)
list AQ7 if Q7=="77"
replace Q7_college = 0 if AQ7=="She is trained and certified as a mutual funds representative."
replace Q7_college = 0 if AQ7=="sdfh"	
replace Q7_vocational = 1 if AQ7=="She is trained and certified as a mutual funds representative."
forval i=7/8 {
	foreach var of varlist Q`i'_dropout-Q`i'_college {
		replace `var'=. if AQ7=="sdfh"
	}
}

* either parent went to college
gen p_uni=(Q7_college==1 | Q8_college==1)
* either parent dropped out
gen p_drop=(Q7_dropout==1 | Q8_dropout==1)


replace Q15C = "." if Q15C=="#NULL!" | Q15C=="99"
destring Q15C, replace
la var Q15C "Number of siblings inf 18"




***** Student is descendent of first nations people
gen p_native=( paborig==1 | pregindian==1 | pindband==1)

gen ind_id=_n
sort ind_id


save survey_vars, replace


